<?php

namespace app\admin\controller\order;

use app\admin\controller\Controllers;
use app\admin\service\order\Order as OrderService;
use app\admin\model\order\Order as OrderModels;

class Order extends Controllers
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(OrderService $service)
    {
        $get = $this->request->get();
        $this->validate($get, 'app\admin\validate\order\Order.index');
        $list = $service->indexPaginate($get);
        $config = config('field.order');
        $this->assign([
            'get' => $get,
            'list' => $list,
            'page' => $list->render(),
            'count' => $list->total(),
            'handle' => $config['handle']
        ]);
        return $this->fetch('order/order/index');
    }

    /**
     * 显示指定的资源
     *
     * @param  OrderModels $model
     * @return \think\Response
     */
    public function read(OrderModels $model)
    {
        $this->assign([
            'data' => $model
        ]);
        return $this->fetch('order/order/show');
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  OrderModels $model
     * @return \think\Response
     */
    public function edit(OrderModels $model)
    {
        $this->assign([
            'data' => $model
        ]);
        return $this->fetch('order/order/edit');
    }

    /**
     * 保存更新的资源
     *
     * @param  orderModels $model
     * @param  OrderService $service
     * @return \think\Response
     */
    public function update(OrderModels $model, OrderService $service)
    {
        $put = $this->request->only(['username','mobile','address','business_remarks'], 'put');
        $this->validate($put, 'app\admin\validate\order\Order.update');
        if ( false === $service->save($model, $put) ) {
            return failed('编辑失败');
        }
        return success();
    }

    /**
     * 新的订单
     *
     * @return \think\Response
     */
    public function handle(OrderService $service)
    {
        $get = $this->request->get();
        $list = $service->handle($get);
        $this->assign([
            'get' => $get,
            'list' => $list,
            'page' => $list->render(),
            'count' => $list->total()
        ]);
        return $this->fetch('order/order/handle');
    }

    /**
     * 处理订单
     *
     * @return \think\Response
     */
    public function complete(int $id, OrderService $service)
    {
        $this->validate(['id'=>$id], 'app\admin\validate\order\Order.complete');
        return $service->complete($id)?
            success():
            failed('处理失败');
    }

    /**
     * 批量处理订单
     *
     * @return \think\Response
     */
    public function batchComplete(OrderService $service)
    {
        $delete = $this->request->only(['ids'], 'post');
        $this->validate($delete, 'app\admin\validate\order\Order.batchComplete');
        return $service->complete($delete['ids'])?
            success():
            failed('处理失败');
    }

    /**
     * 取消订单
     *
     * @return \think\Response
     */
    public function cancel(int $id, OrderService $service)
    {
        $this->validate(['id'=>$id], 'app\admin\validate\order\Order.complete');
        return $service->complete($id, 2)?
            success():
            failed('处理失败');
    }

    /**
     * 批量取消订单
     *
     * @return \think\Response
     */
    public function batchCancel(OrderService $service)
    {
        $delete = $this->request->only(['ids'], 'post');
        $this->validate($delete, 'app\admin\validate\order\Order.batchComplete');
        return $service->complete($delete['ids'], 2)?
            success():
            failed('处理失败');
    }

    /**
     * 统计未处理订单数
     *
     * @return \think\Response
     */
    public function count(OrderService $service)
    {
        return success(['count'=>$service->count()]);
    }

}
